﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using FormTBK.lib;
using FluentData;
using htmlTBK.Models;
using TaoBaoKe.Common;
using System.Text.RegularExpressions;
namespace FormTBK.Function
{
    public partial class SuggestFromWeb : Form
    {
        public SuggestFromWeb()
        {
            InitializeComponent();
        }
        SESuggestion su = new SESuggestion();
        private void button1_Click(object sender, EventArgs e)
        {
            IDbContext dbContext = null;
            while (true)
            {
                try
                {
                    dbContext = new DbContext().ConnectionString("server=.;User ID=sa;Password=111111;database=test;Connection Reset=FALSE", new SqlServerProvider());
                    tbk_KeywordBySeed m = dbContext.Sql("select top 1 * from tbk_KeywordBySeed where isused=0 and kid>=" + this.tbFrom.Text.Trim() + " and kid<" + this.tbTo.Text.Trim() + " order by kid asc").QuerySingle<tbk_KeywordBySeed>();
                    if (m == null)
                    {
                        MessageBox.Show(this, "未发现数据");
                        return;
                    }
                    string key = m.Keyword.UrlEncode();
                    //百度
                    try
                    {
                        string baidu = su.Get("http://www.baidu.com/s?tn=baiduhome_pg&ie=utf-8&f=8&rsv_bp=1&rsv_spt=1&wd=" + key + "&inputT=0", Encoding.UTF8, "<div\\sid=\"rs\">(.*?)</table>", "<a.*?>(?<key>.*?)</a>");
                        m.S_Baidu = baidu;
                    }
                    catch (Exception ex1)
                    {

                        m.S_Baidu = "|error|" + ex1.Message;
                    }
                    //360
                    try
                    {
                        string s_360so = su.Get("http://rs.so.com/?callback=Search.relate.render&q=" + key + "&encodein=utf-8&encodeout=utf-8", Encoding.UTF8, "\"q\":\"(.*)}", "q\":\"(?<key>.*?)\",");
                        string[] ss = s_360so.Split(',');
                        int i = 0; s_360so = "";
                        while (i < ss.Length) s_360so += (i == 0 ? "" : ",") + ss[i++].UnicodeDecode();
                        m.S_360So = s_360so;
                    }
                    catch (Exception ex2)
                    {
                        m.S_360So = "|error|" + ex2.Message;
                    }
                    //必应
                    try
                    {
                        string bing = su.Get("http://cn.bing.com/search?q=" + key + "&go=&qs=bs&form=QBRE", Encoding.UTF8, "</h2><div\\sclass=\"crch\">(.*?)class=\"sb_pag\"", "<a.*?>(?<key>.*?)</a>");
                        m.S_Bing = bing.RemoveHTML();
                    }
                    catch (Exception ex5)
                    {
                        m.S_Bing = "|error|" + ex5.Message;
                    }
                    //谷歌
                    m.S_Google = "";
                    //搜狗
                    try
                    {


                        string sogou = su.Get("http://www.sogou.com/web?ie=utf8&query=" + key + "&_ast=1370927232&_asf=null&w=01029901&p=40040100&dp=1", Encoding.UTF8, "class=\"hint\">(.*?)</table>", "<a.*?>(?<key>.*?)</a>");
                        m.S_Sogou = sogou;
                    }
                    catch (Exception ex3)
                    {

                        m.S_Sogou = "|error|" + ex3.Message;
                    }
                    //腾讯Soso
                    try
                    {
                        string soso = su.Get("http://www.soso.com/q?pid=s.idx&cid=s.idx.se&w=" + key, Encoding.GetEncoding("gb2312"), "ss_c=\"search.hint\"(.*?)</table>", "<a.*?>(?<key>.*?)</a>"); ;
                        m.S_Soso = soso;
                    }
                    catch (Exception ex4)
                    {
                        m.S_Soso = "|error|" + ex4.Message;
                    }
                    m.IsUsed = 1;
                    // dbContext.Update("tbk_KeywordBySeed").Column("S_360So",m.S_360So).Where("kid", m.Kid).Execute();
                    dbContext.Update<tbk_KeywordBySeed>("tbk_KeywordBySeed", m).AutoMap(x => x.Kid).Execute();
                    dbContext.Dispose();
                }
                catch (Exception wx)
                {
                    
                    throw wx;

                }
                finally {
                    dbContext.Dispose();
                }
           

            }
            MessageBox.Show(this,"完成");

        }
    }
}
